package by.demchenko.sample.services.impl;

import by.demchenko.sample.dataaccess.base.PersonDao;
import by.demchenko.sample.datamodel.Person;
import by.demchenko.sample.datamodel.enums.UserType;
import by.demchenko.sample.services.PersonService;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;

@Service
public class PersonServiceImpl implements PersonService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    @Inject
    private PersonDao dao;

    @Override
    public Person get(Integer id, boolean fetchAll) {
        return dao.getById(id, fetchAll);
    }

    @Override
    public void save(Person person) {
        Validate.notNull(person, "Object to save can’t be null");
        dao.insert(person);
    }

    @Override
    public void saveOrUpdate(Person person) {
        if (person.getId() == null) {
            LOGGER.debug("Save new: {}", person);
            dao.insert(person);
        } else {
            LOGGER.debug("Update: {}", person);
            dao.update(person);
        }
    }

    @Override
    public List<Person> getPersonsByType(UserType userType, boolean fetchJoins) {
        return dao.getPersonsByType(userType, fetchJoins);
    }

    @Override
    public <C> List<Person> getStudentsByCriteria(C criterion) {
        return dao.getStudentsByCriteria(criterion);
    }

    @Override
    public long getCount(UserType userType) {
        return dao.getCount(userType);
    }

    @Override
    public void delete(Person person) {
        dao.delete(person.getId());
    }
}